package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.blackbox.Broadcast.MineBroadcast;
import com.example.blackbox.Net.NetTool;
import com.example.blackbox.R;
import com.example.blackbox.Service.MainService;

import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;

public class LoginActivity extends Activity {
    private Button sign;
    EditText mEtUsername;
    EditText mEtpassword;
    TextView register;
    Handler handler;
    String flag;
    SharedPreferences sp;
    String un,pwd;
    String username,password;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        startService();
        initUI();
        username=sp.getString("username",null);
        password=sp.getString("password",null);
        mEtUsername.setText(username);
        mEtpassword.setText(password);

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtUsername.getText().toString())) {
                    Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                }else {
                    un=mEtUsername.getText().toString();
                    pwd=mEtpassword.getText().toString();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            flag = NetTool.signChecking(un, pwd);
                            Message m=handler.obtainMessage();
                            handler.sendMessage(m);
                        }
                    }).start();

                }

            }
        });

        IntentFilter filter=new IntentFilter();
        filter.addAction("SIGN_INFO_ACTION");
        MineBroadcast myBroadCastReceiver=new MineBroadcast(LoginActivity.this);
        registerReceiver(myBroadCastReceiver,filter);

        handler=new Handler(){
            @Override
            public void handleMessage( Message msg) {
                if ("ok".equals(flag)){
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                }

            }
        };

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegistereActivity.class));
            }
        });
    }

    public void initUI(){
        sign=(Button)findViewById(R.id.login_bt_sign);
        mEtUsername=(EditText)findViewById(R.id.login_et_username);
        mEtpassword=(EditText)findViewById(R.id.login_et_password);
        register=(TextView) findViewById(R.id.login_tv_register);
        sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    public void startService() {
        startService(new Intent(getBaseContext(), MainService.class));
    }
}
